#!/usr/bin/env bash

SCRIPT_CONF="/config/setting.conf"

LOAD_CONF() {
    RMTASK="$(grep ^remove-task "${SCRIPT_CONF}" | cut -d= -f2-)"
    MOVE="$(grep ^move-task "${SCRIPT_CONF}" | cut -d= -f2-)"
    CF="$(grep ^content-filter "${SCRIPT_CONF}" | cut -d= -f2-)"
    DET="$(grep ^delete-empty-dir "${SCRIPT_CONF}" | cut -d= -f2-)"
    TOR="$(grep ^handle-torrent "${SCRIPT_CONF}" | cut -d= -f2-)"
    RRT="$(grep ^remove-repeat-task "${SCRIPT_CONF}" | cut -d= -f2-)"
    MPT="$(grep ^move-paused-task "${SCRIPT_CONF}" | cut -d= -f2-)"
}

SED_CONF() {
    # 复制用户配置文件
    cp /aria2/conf/setting.conf /config/setting.conf.new
    # 复原用户配置文件参数
    sed -i "s@^\(remove-task=\).*@\1${RMTASK}@" /config/setting.conf.new
    sed -i "s@^\(move-task=\).*@\1${MOVE}@" /config/setting.conf.new
    sed -i "s@^\(content-filter=\).*@\1${CF}@" /config/setting.conf.new
    sed -i "s@^\(delete-empty-dir=\).*@\1${DET}@" /config/setting.conf.new
    sed -i "s@^\(handle-torrent=\).*@\1${TOR}@" /config/setting.conf.new
    sed -i "s@^\(remove-repeat-task=\).*@\1${RRT}@" /config/setting.conf.new
    sed -i "s@^\(move-paused-task=\).*@\1${MPT}@" /config/setting.conf.new
    # 如某项参数不存在，则使用默认参数，防止程序运行出错
    if [[ -z "${RMTASK}" ]]; then
        sed -i "s@^\(remove-task=\).*@\1rmaria@" /config/setting.conf.new
    elif [[ -z "${MOVE}" ]]; then
        sed -i "s@^\(move-task=\).*@\1false@" /config/setting.conf.new
    elif [[ -z "${CF}" ]]; then
        sed -i "s@^\(content-filter=\).*@\1false@" /config/setting.conf.new
    elif [[ -z "${DET}" ]]; then
        sed -i "s@^\(delete-empty-dir=\).*@\1true@" /config/setting.conf.new
    elif [[ -z "${TOR}" ]]; then
        sed -i "s@^\(handle-torrent=\).*@\1backup-rename@" /config/setting.conf.new
    elif [[ -z "${RRT}" ]]; then
        sed -i "s@^\(remove-repeat-task=\).*@\1true@" /config/setting.conf.new
    elif [[ -z "${MPT}" ]]; then
        sed -i "s@^\(move-paused-task=\).*@\1false@" /config/setting.conf.new
    fi

    rm -f /config/setting.conf
    mv /config/setting.conf.new /config/setting.conf
}

LOAD_CONF
